package com.cskaoyan.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.order.constant.OrderRetCode;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.service.OrderCoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * @author pjr
 * @version 1.0.0
 * @description TODO
 * @date 2022/6/17 20:39
 */
@RestController
@RequestMapping("/shopping")
public class OrderCoreController {
    @Autowired
    OrderCoreService orderCoreService;


    @PostMapping("/order")
    public ResponseData createOrder(@RequestBody CreateOrderRequest request, HttpServletRequest servletRequest) {
        // 获取用户id，这里UserId前端是没有传过来的，需要用户登录后从header中取出
        String userInfo = (String) servletRequest.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());
        request.setUserId(uid);

        // 设置uniqueKey
        request.setUniqueKey(UUID.randomUUID().toString());

        // 调用service层具体的业务【返回的是订单id，这里为了统一也封装为response对象】
        CreateOrderResponse response = orderCoreService.createOrder(request);

        // 根据业务执行的不同状态返回不同的信息给前端【视图层的功能】
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getOrderId());
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    /**
     * 删除订单
     * @param id
     * @return
     */
    @DeleteMapping("/order/{id}")
    public ResponseData deleteOrder(@PathVariable("id") String id) {

        DeleteOrderRequest request = new DeleteOrderRequest();

        request.setOrderId(id);

        // 调用service处理请求
        DeleteOrderResponse response = orderCoreService.deleteOrder(request);

        if (OrderRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功，直接返回前端需要的数据
            return new ResponseUtil().setData(response);
        }
        // 调用失败
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /**
     * 取消订单
     * @param order
     * @return
     */
    @PostMapping("/cancelOrder")
    public ResponseData cancelOrder(@RequestBody Order order) {
        String orderId = order.getOrderId();
        //  String orderId = servletRequest.getParameter("orderId");
        CancelOrderRequest request = new CancelOrderRequest();
        request.setOrderId(orderId);

        // 调用service处理请求
        CancelOrderResponse response = orderCoreService.cancelOrder(request);

        if (OrderRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功，直接返回前端需要的数据
            return new ResponseUtil().setData(response);
        }
        // 调用失败
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

}
